from sls_gc_MissionsWaypoint_ui import Ui_MissionsWaypoint
from PyQt6.QtWidgets import QWidget,QApplication
from PyQt6.QtGui import QBrush,QColor
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QDialog
from sls_gc_agent import sls_Agent
import sys
import sls_coordinationConvert as sls_c

class gc_MissionsWaypoint(QDialog):
    def __init__(self,parent=None,fly:str=None,index:int=0):
        super().__init__(parent)
        self.add_index = index # 新增的任务索引，默认为0，即添加到列表的最前面，也可以根据需要修改为其他值，如self.ui.listWidget.currentRow()，表示添加到当前选中的任务后面，或者其他自定义的索引值。       
        self.ui = Ui_MissionsWaypoint()
        self.ui.setupUi(self)
        self.parent = parent
        self.fly = fly
        self.setWindowTitle(f"{fly} 航迹点飞行")
        self.latitude = None
        self.longitude = None
        self.ui.pushButton.clicked.connect(self.pushButton_clicked)
        self.parent.parent.notifyClass.add_notify_Cordinate(self.updateCoordinate)

    def updateCoordinate(self,latitude,longitude):
        #在国内地图上显示标记点，按照GCJ02坐标系显示
        self.parent.parent.add_marker(latitude,longitude,f'{self.fly}temppoint')
        #转换为WGS84坐标系存储
        self.longitude,self.latitude = sls_c.gcj02_to_wgs84(longitude, latitude)
        #在界面显示显示WGS84坐标
        self.ui.positionLabel.setText(f"WGS84坐标，经度：{self.longitude} 纬度：{self.latitude}")

        
    
    def pushButton_clicked(self):
        #删除地图上的显示点
        #todo:
        self.parent.parent.delete_marker(f'{self.fly}temppoint')
        agent:sls_Agent = self.parent.parent.agent
        #组合并发送
        waypointDict = {
            "missionsType": "MissionsWaypoint",
            "datasets":{
                "latitude":self.latitude,
                "longitude":self.longitude,
            }
        }
        #agent.publish_MS_appendMission(self.fly,takeoffDict)
        agent.publish_MS_addMission(self.fly,waypointDict,self.add_index)
        self.parent.ask_missionslist()
        self.close()

    def closeEvent(self, event):
        # 删除地图上的显示点
        self.parent.parent.delete_marker(f'{self.fly}temppoint')
        event.accept()  # 接受关闭事件，确保窗口正确关闭